package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author:weng
 * @data:2022/11/3
 * @description:
 */

public class LockAndSensorAct  extends Activity implements SensorEventListener {
    public static final String TAG = "SensorTest";
    private SensorManager sensorManager;
    private TextView tv;
    private PowerManager localPowerManager = null;// 电源管理对象
    private PowerManager.WakeLock localWakeLock = null;// 电源锁
    private  RxJavaFactory rxJavaFactory ;

    @SuppressLint("InvalidWakeLockTag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        tv = (TextView) findViewById(R.id.tv);
        localPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
        // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
        localWakeLock = this.localPowerManager.newWakeLock(32, "hahaha");// 第一个参数为电源锁级别，第二个是日志tag
        rxJavaFactory = new RxJavaFactory();
//        rxJavaFactory.onCreate();
        rxJavaFactory.just();
    }
    @Override
    protected void onResume() {
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
                SensorManager.SENSOR_DELAY_UI);
        super.onResume();
    }
    @Override
    protected void onPause() {
        sensorManager.unregisterListener(this);
        super.onPause();
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float[] values = event.values;
        switch (event.sensor.getType()) {
            case Sensor.TYPE_PROXIMITY:
                tv.setText(values[0] + "");
                if (values[0] == 0.0) {// 贴近手机
                    System.out.println("hands up");
                    Log.d(TAG, "hands up in calling activity");
                    if (localWakeLock.isHeld()) {
                    } else {
                        localWakeLock.acquire();// 申请设备电源锁
                    }
                    vibrate(getBaseContext(),100);
                                Observable.interval(1,1, TimeUnit.SECONDS)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribe(aLong -> {
                                    Log.e("tag","倒计时开始--->"+aLong);
                                });
                } else {// 远离手机
                    System.out.println("hands moved");
                    Log.d(TAG, "hands moved in calling activity");
                    if (localWakeLock.isHeld()) {

                    } else {

                        localWakeLock.setReferenceCounted(false);
                        localWakeLock.release(); // 释放设备电源锁
                    }
                    break;
                }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }


    /**
     * 震动手机
     * @param context 上下文
     * @param milliseconds 震动时间（毫秒）
     */
    @SuppressLint("MissingPermission")
    public static Vibrator vibrate(Context context, long milliseconds) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        if (vibrator != null){
            vibrator.vibrate(milliseconds);
        }
        return vibrator;
    }
}


